/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.gis.eemc.dao;

import net.gis.eemc.domain.Faculty;
import net.gis.eemc.domain.Department;
import net.gis.eemc.domain.HibernateUtil;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author vinser52
 */
public class DepartmentDAOImpl implements DepartmentDAO {

    Session session = null;

    public DepartmentDAOImpl() {
        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
    }

    @Override
    public void addDepartment(Department dept) {
        org.hibernate.Transaction tx = null;

        try {
            this.session = HibernateUtil.getSessionFactory().getCurrentSession();

            tx = session.beginTransaction();
            session.save(dept);
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        }
    }

    @Override
    public List<Department> listDeptartment() {
        return getDepartments("from Department as department");
    }

    @Override
    public List<Department> listDeptartment(Integer fac_id) {
        return getDepartments("from Department as department where department.faculty.idfaculty = \"" + fac_id.toString() + "\"");
    }

    @Override
    public List<Department> listDeptartment(Faculty fac) {
        return listDeptartment(fac.getIdfaculty());
    }

    @Override
    public void removeDepartment(Integer id) {
        org.hibernate.Transaction tx = null;

        try {
            this.session = HibernateUtil.getSessionFactory().getCurrentSession();

            tx = session.beginTransaction();
            String hqlDelete = "delete Department d where d.idDep = :id";
            Query q = session.createQuery(hqlDelete);
            q.setString("id", id.toString());
            q.executeUpdate();
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        }
    }

    @Override
    public void updateDepartment(Department dep) {
        org.hibernate.Transaction tx = null;
        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            tx = session.beginTransaction();
            session.update(dep);
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        }
    }

    @Override
    public List<Department> getDepartmentWithTitle(String Title) {
        return getDepartments("from Department as department where department.title=\"" + Title + "\"");
    }

    @Override
    public List<Department> getDepartmentWithShortName(String shortName) {
        return getDepartments("from Department as department where department.shortName=\"" + shortName + "\"");
    }

    @Override
    public Department getDepartmentWithID(Integer id) {
        return getDepartments("from Department as department where department.idDep=\"" + id.toString() + "\"").get(0);
    }

    private List<Department> getDepartments(String query) {
        List<Department> deptList = null;
        org.hibernate.Transaction tx = null;

        try {
            this.session = HibernateUtil.getSessionFactory().getCurrentSession();

            tx = session.beginTransaction();
            Query q = session.createQuery(query);
            deptList = (List<Department>) q.list();
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        }
        return deptList;
    }
}
